/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.io.Serializable;
import java.util.ArrayList;

/**
 *
 * @author ThanhToan
 */
public class ShoppingCardModel implements Serializable{
    private static final long serialVersionUID = 1L;
    private ArrayList<OrderDetailsModel> shoppingCard;

    public ArrayList<OrderDetailsModel> getShoppingCard() {
        return shoppingCard;
    }

    public void setShoppingCard(ArrayList<OrderDetailsModel> shoppingCard) {
        this.shoppingCard = shoppingCard;
    }

    @SuppressWarnings("element-type-mismatch")
    public boolean checkProduct(String productID) {
        return shoppingCard.contains(productID);
    }

    @SuppressWarnings("element-type-mismatch")
    public void add(OrderDetailsModel orderModel) {
        if(shoppingCard.contains(orderModel.getProductID())) { // new product
            int num = Integer.parseInt(orderModel.getNumber()) + 1;
            orderModel.setNumber(String.valueOf(num));
        }
        else{ // product exit
            shoppingCard.add(orderModel);
        }
    }

    @SuppressWarnings("element-type-mismatch")
    public void update(OrderDetailsModel orderModel, String newNum) {
        if(shoppingCard.contains(orderModel.getProductID())){
            int num = Integer.parseInt(orderModel.getNumber()) + Integer.parseInt(newNum);
            orderModel.setNumber(String.valueOf(num));
        }
    }

    @SuppressWarnings("element-type-mismatch")
    public void remove(String productID){
        if(shoppingCard.contains(productID)){
            shoppingCard.remove(productID);
        }
    }

    public String getValue(){
        float _total = 0;
        for(OrderDetailsModel item : shoppingCard){
            _total += Float.parseFloat(item.getNumber()) * Float.parseFloat(item.getPrice());
        }
        String total = String.valueOf(_total);
        return total;
    }

    public void removeAll() {
        shoppingCard.clear();
    }
}
